// pages/mall/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    background: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 4000,
    duration: 500,
    indicatorColor:'#fff',
    indicatorActiveColor:'#ff8603' ,// 选择指示点颜色
    mallListTab:[
      {
        id:1,
        tabTitle:'古诗词',
        tabIcon:'icon-shu_1',
        category:'',
        userName:'古',
        color:'#00a2ff'
      },
      {
        id:7,
        tabTitle:'唐诗三百首',
        tabIcon:'icon-shuji',
        category:'6',
        userName:'唐',
        color:'#59aaff'
      },
      {
        id:6,
        tabTitle:'爱国',
        tabIcon:'icon-aiguoxin',
        category:5,
        userName:'高',
        color:'red'
      },
      {
        id:4,
        tabTitle:'小学',
        tabIcon:'icon-1-09',
        category:1,
        userName:'小',
        color:'#03d9ff'
      },
      {
        id:5,
        tabTitle:'初中',
        tabIcon:'icon-gongbanchuzhong',
        category:2,
        userName:'初',
        color:'#ff8603'
      },
      {
        id:6,
        tabTitle:'高中',
        tabIcon:'icon-gaozhongjieduanjiaoyumaoruxueshuai',
        category:3,
        userName:'高',
        color:'tomato'
      },
      {
        id:6,
        tabTitle:'爱情',
        tabIcon:'icon-aiqing',
        category:4,
        userName:'爱情',
        color:'#6b79f5'
      },
      {
        id:2,
        tabTitle:'敬请期待...',
        tabIcon:'icon-allicon-156',
        userName:'敬请',
        color:'#00a2ff'
      }
    ],
    userTitleBox:[
      '原来他古诗词这么好，是因为这个小程序',
      '以为再也不会为古诗词担心了，小初高都有，还有注解！',
      '这真是一个适合学生的工具，以后再也不用担心小孩的诗文了。',
    ],
    // 热门推荐
    userList:[]
  },

  /**
   * 
   * @param {tab点击事件} options 
   */
  onTab(e){
    let item = e.currentTarget.dataset.item
    if(item.id==1){
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==1){ // 小学
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==2){ // 初中
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==3){ // 高中
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==4){ // 爱情
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==5){ // 爱国
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }else if(item.category==6){ // 唐诗三百首
      wx.navigateTo({
        url: `/pages/mood/index?category=${item.category}`,
      })
    }
    
  },

  // 搜索
  onSerch(){
    wx.navigateTo({
      url: '/pages/serch/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options._id) {
      wx.navigateTo({
        url: '/pages/info/index?_id=' + options._id,
      })
    }
    let that = this
    wx.cloud.callFunction({ // 获取轮播图
      name:'getSlideshow'
    })
    .then(res=>{ // 保存到本地
      wx.setStorageSync('background', res.result.data)
      that.setData({
        background:res.result.data
      })
    })

    this.getMiscellanies(20,0,'')

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },

    /**
   * 请求数据源
   */
  getMiscellanies(pagesTotal,pages,serchText){
    let _this = this
    let category = this.data.category
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({ // 获取数据
      name:'getMiscellanies',
      data:{
        pagesTotal,
        pages,
        category,
        poem:serchText || ''
      }
    }).then(res=>{
      let oldData = _this.data.userList
      let newData = oldData.concat(res.result.data)
      _this.setData({
        userList:newData
      })
      /**
       * 判断数据是否存在，如果没有了就显示没有更多的数据
       */
      if(res.result.data.length==0 ||res.result.data.length<pagesTotal){
        _this.setData({
          isFalse:true
        })
      }
    })
    .finally(()=>{
      wx.hideLoading()
    })
  },

  /**
   * 
   * @param {跳转到详情页面} e 
   */
  onUserInfo(e){
    let {_id} = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/info/index?_id=${_id}`
    })
  },

  /**
   * 祝福
   */
  onWish() {
    wx.navigateToMiniProgram({
      appId: 'wx25c0b7004ed7ea96',
      path: 'pages/mall/index',
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userTitleBox = this.data.userTitleBox
    let background = this.data.background
    let random = Math.floor(Math.random()*userTitleBox.length)
    let randomBg = Math.floor(Math.random()*background.length)

    return {
      title:userTitleBox[random],
      path:'/pages/mall/index',
      imageUrl:background[randomBg].fileID
    }
  }
})